package com.online.usermanagesystem.service.impl;

import com.online.usermanagesystem.dao.CompanyMapper;
import com.online.usermanagesystem.entity.Company;
import com.online.usermanagesystem.service.CompanyManageService;
import com.online.usermanagesystem.utils.ResultUtil;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 客户管理的处理类
 *
 * @create 2018-04-24 10:35
 */
@Component
public class CompanyManageServiceImpl implements CompanyManageService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Object query(Integer id) {
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Company company;
        try {
            company = companyMapper.query(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (null == company) {
            return ResultUtil.NOT_FOUND;
        }

        return company;
    }

    @Override
    public Object add(Company company) {
        if ((null == company) || (null == company.getName()) || (null == company.getCode())) {
            return ResultUtil.BAD_REQUEST;
        }

        Integer count;
        try {
            count = companyMapper.countByName(company.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 != count) {
            return ResultUtil.CONFLICT;
        }

        if (null == company.getAddress()) {
            company.setAddress("");
        }

        if (null == company.getTelephone()) {
            company.setTelephone("");
        }

        try {
            companyMapper.insert(company);
        } catch (DuplicateKeyException e) {
            return ResultUtil.CONFLICT;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.returnKV("id", company.getId());
    }

    @Override
    public Object del(Integer id) {
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Integer count;
        try {
            count = companyMapper.countById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 != count) {
            return ResultUtil.NOT_FOUND;
        }

        try {
            companyMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }

    @Override
    public Object modify(Company company) {
        if (null == company) {
            return ResultUtil.BAD_REQUEST;
        }

        Integer id = company.getId();
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Integer count;
        try {
            count = companyMapper.countById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 != count) {
            return ResultUtil.NOT_FOUND;
        }

        try {
            companyMapper.update(company);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }

    @Override
    public Object list(String keyword, Integer index, Integer count) {
        if (null == index) {
            index = 0;
        }

        if (null == count) {
            count = 64;
        }

        List<Company> list;
        try {
            if (null == keyword) {
                list = companyMapper.querys(index, count);
            } else {
                list = companyMapper.search(keyword.concat("%"), index, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return list;
    }
}
